package com.hcf.admin.remote;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hcf.admin.common.convention.result.Result;
import com.hcf.admin.remote.dto.req.*;
import com.hcf.admin.remote.dto.resp.ListShortLinkCountRespDTO;
import com.hcf.admin.remote.dto.resp.ShortLinkCreateRespDTO;
import com.hcf.admin.remote.dto.resp.ShortLinkPageRespDTO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短链接中台远程调用服务
 */
public interface ShortLinkProjectService {
    /**
     * 分页查询短链接
     * @param requestParam
     * @return
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam){
        // 参数用map携带过去
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("gid", requestParam.getGid());
        requestMap.put("current", requestParam.getCurrent());
        requestMap.put("size", requestParam.getSize());
        String result = HttpUtil.get("http://127.0.0.1:8001/api/shortlink/project/v1/page", requestMap);
        return JSON.parseObject(result, new TypeReference<Result<IPage<ShortLinkPageRespDTO>>>(){});
    }


    /**
     * 原始创建短链接
     * @param requestParam
     * @return
     */
    default Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreateReqDTO requestParam){
        String result = HttpUtil.post("http://127.0.0.1:8001/api/shortlink/project/v1/create", JSON.toJSONString(requestParam));
        return JSON.parseObject(result, new TypeReference<Result<ShortLinkCreateRespDTO>>(){});
    }


    /**
     * 修改短链接
     * @param requestParam 修改短链接的参数
     * @return
     */
    default Void updateShortLink(ShortLinkUpdateReqDto requestParam){
        HttpUtil.post("http://127.0.0.1:8001/api/shortlink/project/v1/update", JSON.toJSONString(requestParam));
        return null;
    }


    /**
     * 查询分组内短链接数量
     * @param requestParam
     * @return
     */
    default Result<List<ListShortLinkCountRespDTO>> listShortLinkCount(List<String> requestParam){
        // 参数用map携带过去
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("requestParam", requestParam);
        String result = HttpUtil.get("http://127.0.0.1:8001/api/shortlink/project/v1/count",requestMap);
        return JSON.parseObject(result, new TypeReference<Result<List<ListShortLinkCountRespDTO>>>(){});
    }

    /**
     * 获取原始链接标题
     * @param url
     * @return
     */
    default Result<String> getUrlTitle(String url) {
        String result =  HttpUtil.get("http://127.0.0.1:8001/api/shortlink/project/v1/title?url" + url);
        return JSON.parseObject(result, new TypeReference<Result<String>>(){});
    }

    /**
     * 回收站新增
     */
    default Result<Void> saveRecycleBin(RecycleBinSaveReqDto requestParam) {
        String result =  HttpUtil.post("http://127.0.0.1:8001/api/shortlink/project/v1/recycle-bin/save", JSON.toJSONString(requestParam));
        return JSON.parseObject(result, new TypeReference<Result<Void>>(){});
    }


    /**
     * 分页查询回收站短链接
     * @param requestParam
     * @return
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageRecycleBin(RecycleBinPageReqDTO requestParam){
        // 参数用map携带过去
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("gidList", requestParam.getGidList());
        requestMap.put("current", requestParam.getCurrent());
        requestMap.put("size", requestParam.getSize());
        String result = HttpUtil.get("http://127.0.0.1:8001/api/shortlink/project/v1/recycle-bin/page",requestMap);
        return JSON.parseObject(result, new TypeReference<Result<IPage<ShortLinkPageRespDTO>>>(){});
    }

    /**
     * 恢复回收站短链接
     * @param requestParam
     * @return
     */
    default Result<Void> revocerShortLink(RecycleBinRecoverReqDTO requestParam){
        String result =  HttpUtil.post("http://127.0.0.1:8001/api/shortlink/project/v1/recycle-bin/recover", JSON.toJSONString(requestParam));
        return JSON.parseObject(result, new TypeReference<Result<Void>>(){});
    }


    /**
     * 删除回收站短链接
     * @param requestParam
     * @return
     */
    default Result<Void> removeShortLink(RecycleBinRemoveReqDTO requestParam){
        String result =  HttpUtil.post("http://127.0.0.1:8001/api/shortlink/project/v1/recycle-bin/remove", JSON.toJSONString(requestParam));
        return JSON.parseObject(result, new TypeReference<Result<Void>>(){});
    }
}
